/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mvc;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import javax.json.JsonObject;

/**
 *
 * @author William
 */
public abstract class Controller {

    private final String name;
    protected Map<String, Resource> resources;
    protected Map<String, Event> events;
    
    public Controller(String name) {
        this.name = name;
        this.resources = new HashMap();
        this.events = new HashMap();
    }

    //Collections: push and remove
    public void registerResource(Resource rsc) {
        this.resources.put(rsc.getName(), rsc);
    }
    
    public void removeResource(String entry) {
        this.resources.remove(entry);
    }
    
    public void registerEvent(Event evt) {
        this.events.put(evt.getName(), evt);
    }
    
    public void removeEvent(String entry) {
        this.events.remove(entry);
    }
    
    //Other methods
    public boolean evoke(String entry, JsonObject params) {
        Event evt = this.events.get(entry);
        if(evt == null) {
            return false;
        }
        return evt.handle(params);
    }
    
    public JsonObject require(String entry, JsonObject params) {
        Resource rsc = this.resources.get(entry);
        if(rsc == null) {
            return null;
        }
        return rsc.obtain(params);
    }
    
    //Getters and setters
    public String getName() {
        return this.name;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 67 * hash + Objects.hashCode(this.name);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Controller other = (Controller) obj;
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        return true;
    }
    
    
}
